<?php

class alumni_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = "alumni";
    }

    function insert($alumninama, $alumniangkatan, $alumnialamatasal, $alumnikotaasal, $alumnipropinsiasal, $alumnialamatdomisili, $alumnikotadomisili, $alumnipropinsidomisili, $alumnicontact, $alumniemail, $alumnipassword, $alumniischange, $alumnijenkel, $alumnistatus, $alumnitype, $alumniparentid, $alumnikeahlian, $alumniportofolio, $createdby, $updatedby, $isadmin = 0) {
        $dt = array("alumninama" => $alumninama,
            "alumniangkatan" => $alumniangkatan,
            "alumnialamatasal" => $alumnialamatasal,
            "alumnikotaasal" => $alumnikotaasal,
            "alumnipropinsiasal" => $alumnipropinsiasal,
            "alumnialamatdomisili" => $alumnialamatdomisili,
            "alumnikotadomisili" => $alumnikotadomisili,
            "alumnipropinsidomisili" => $alumnipropinsidomisili,
            "alumnicontact" => $alumnicontact,
            "alumniemail" => $alumniemail,
            "alumnipassword" => md5($alumnipassword),
            "alumniischange" => $alumniischange,
            "alumnijenkel" => $alumnijenkel,
            "alumnistatus" => $alumnistatus,
            "alumnitype" => $alumnitype,
            "alumniparentid" => $alumniparentid,
            "alumnikeahlian" => $alumnikeahlian,
            "alumniportofolio" => $alumniportofolio,
            "isadmin" => $isadmin,
            "created" => date("Y-m-d H:i:s"),
            "createdby" => $createdby,
            "updatedby" => $updatedby);
        $this->db->insert($this->tableName, $dt);
        return $this->db->insert_id();
    }

    function update($alumniid, $dt, $where = "") {
        $this->db->where("alumniid", $alumniid);
        if ($where != "")
            $this->db->where($where);
        return $this->db->update($this->tableName, $dt);
    }

    function delete($alumniid, $where = "") {
        $this->db->where("alumniid", $alumniid);
        if ($where != "")
            $this->db->where($where);
        $this->db->delete($this->tableName);
    }

    function getById($alumniid, $where = "") {
        $this->db->select($this->tableName.".*, pa.propinsinama propinsiasal, pd.propinsinama propinsidomisili, ka.kabupatennama  kotaasal, kd.kabupatennama  kotadomisili");
        $this->db->from($this->tableName);
        $this->db->join("propinsi pa", $this->tableName.".alumnipropinsiasal = pa.propinsiid", "left");
        $this->db->join("propinsi pd", $this->tableName.".alumnipropinsidomisili = pd.propinsiid", "left");
        $this->db->join("kabupaten ka", $this->tableName.".alumnikotaasal = ka.kabupatenid and " . $this->tableName . ".alumnipropinsiasal = ka.propinsiid", "left");
        $this->db->join("kabupaten kd", $this->tableName.".alumnikotadomisili = kd.kabupatenid and " . $this->tableName . ".alumnipropinsidomisili = kd.propinsiid", "left");
        $this->db->where("alumniid", $alumniid);
        if ($where != "")
            $this->db->where($where);
        return $this->db->get()->row();
    }

    function getAll($startpage = "0", $perpage = "10", $where = "", $orderby = "") {
        $this->db->select($this->tableName.".*, pa.propinsinama propinsiasal, pd.propinsinama propinsidomisili, ka.kabupatennama  kotaasal, kd.kabupatennama  kotadomisili");
        if ($where != "")
            $this->db->where($where);
        $this->db->limit($perpage, $startpage);
        if ($orderby != "")
            $this->db->order_by($orderby);
        $this->db->from($this->tableName);
        $this->db->join("propinsi pa", $this->tableName.".alumnipropinsiasal = pa.propinsiid");
        $this->db->join("propinsi pd", $this->tableName.".alumnipropinsidomisili = pd.propinsiid");
        $this->db->join("kabupaten ka", $this->tableName.".alumnikotaasal = ka.kabupatenid and " . $this->tableName . ".alumnipropinsiasal = ka.propinsiid");
        $this->db->join("kabupaten kd", $this->tableName.".alumnikotadomisili = kd.kabupatenid and " . $this->tableName . ".alumnipropinsidomisili = kd.propinsiid");
        return $this->db->get()->result();
    }

    function getAllCount($where = "") {
        $this->db->select("COUNT(*) JUMLAH");
        $this->db->from($this->tableName);
        if ($where != "")
            $this->db->where($where);
        return $this->db->get()->row()->JUMLAH;
    }
    
    function login($email, $pass){
		$this->db->select("alumniid, alumninama, alumniangkatan, alumniischange, isadmin");
        return $this->db->where(array("alumniemail" => $email, "alumnipassword" => md5($pass)))->get($this->tableName)->row();
    }
    
    function getKerabat($alumniid){
        $this->db->select($this->tableName.".*, pd.propinsinama propinsidomisili, kd.kabupatennama  kotadomisili");
        $this->db->from($this->tableName);
        $this->db->join("propinsi pd", $this->tableName.".alumnipropinsidomisili = pd.propinsiid", "left");
        $this->db->join("kabupaten kd", $this->tableName.".alumnikotadomisili = kd.kabupatenid and " . $this->tableName . ".alumnipropinsidomisili = kd.propinsiid", "left");
        $this->db->where("alumniparentid", $alumniid);
        $this->db->order_by("alumninama");
        return $this->db->get()->result();
    }
    function getKerabatById($alumniid){
        $this->db->select($this->tableName.".*, pd.propinsinama propinsidomisili, kd.kabupatennama  kotadomisili, a.alumninama kerabatnama, a.alumniangkatan kerabatangkatan");
        $this->db->from($this->tableName);
        $this->db->join("alumni a", $this->tableName.".alumniparentid = a.alumniid");
        $this->db->join("propinsi pd", $this->tableName.".alumnipropinsidomisili = pd.propinsiid", "left");
        $this->db->join("kabupaten kd", $this->tableName.".alumnikotadomisili = kd.kabupatenid and " . $this->tableName . ".alumnipropinsidomisili = kd.propinsiid", "left");
        $this->db->where($this->tableName.".alumniid", $alumniid);
        $this->db->order_by("alumninama");
        return $this->db->get()->row();
    }

}

?>